<?
	$type = fgsBase::getParam(0);
	if ($type != 'auth' && $type != 'anon') 
		$type = 'auth';

?>
<script type="text/javascript" src="<?=fgsBase::getBaseUrl()?>js/ths.js">
</script>
<h2><?=tr('Statistics')?></h2>
<p>
<? 
	if ($type == 'anon') {
		echo fgsBase::linkPage(tr('Authenticated players'), 'stats', array('auth'));
		echo ' - ';
		echo tr('Anonymous players');
	} else {
		echo tr('Authenticated players');
		echo ' - ';
		echo fgsBase::linkPage(tr('Anonymous players'), 'stats', array('anon'));
	}
?>
</p>
<h3><?=tr('Most active players')?></h2>
<?
	$table = new htmlTable();
	$table->setHeader('rank', array(
		'title' => tr('Rank'),
		'width' => '6ex',
	));
	$table->setHeader('name', array(
		'title' => tr('Name'),
	));
	$table->setHeader('nb', array(
		'title' => tr('Nb matchs'),
		'width' => '14ex',
	));

	$stats = fgsStats::getPlayersMatchs(($type == 'auth'));
	foreach ($stats as $k => $player) {
		$table->set('rank', 1+$k);
		if ($type == 'auth')
			$table->set('name', fgsBase::linkPage($player['name'], 'player', array($player['name'])));
		else
			$table->set('name', $player['name']);
		$table->set('nb', $player['count']);
		$table->push();
	}
	echo $table->render();
?>


<h3><?=tr('Best players')?></h2>
<?
	$table = new htmlTable();
	$table->setHeader('rank', array(
		'title' => tr('Rank'),
		'width' => '6ex',
	));
	$table->setHeader('name', array(
		'title' => tr('Name'),
	));
	$table->setHeader('nb', array(
		'title' => tr('Nb matchs'),
		'width' => '14ex',
	));
	$table->setHeader('score', array(
		'title' => tr('Score'),
		'width' => '10ex',
	));

	$stats = fgsStats::getPlayersScores(($type == 'auth'));

	echo '<div id="stats_icons">';
	foreach ($stats as $game_name => $players) {
		$game = fgsGames::get($game_name);
		$game_logo = sprintf('<img src="%s"/>', fgsBase::link('game', array($game['gid'], 'logo', 'icon16')));
		printf('<a href="#stats_%s">%s</a>', $game_name, $game_logo);
	}
	echo '</div>';

	foreach ($stats as $game_name => $players) {

		$game = fgsGames::get($game_name);
		printf('<a id="stats_%s"/>', $game_name);

		$game_logo = '';
		$game_url = '';
		if (!empty($game['logo_data'])) 
			$game_logo = sprintf('<img src="%s"/>', fgsBase::link('game', array($game['gid'], 'logo')));
		if (!empty($game['url']))
			$game_url = sprintf('<span class="info"><a href="%s">%s</a></span>', $game['url'], $game['url']);
		printf('<h4 class="icon">%s<a href="%s">%s</a><span class="info">%s</span></h4>', 
			$game_logo, 
			fgsBase::link('game', array($game['gid'])),
			$game['name'], 
			$game_url
		);


		$table->clear();
		$rank = 0;
		foreach ($players as $name => $player) {
			$rank++;
			$table->set('rank', $rank);
			if ($type == 'auth')
				$table->set('name', fgsBase::linkPage($name, 'player', array($name)));
			else
				$table->set('name', $name);
			$table->set('nb', $player['count']);
			$table->set('score', $player['score']);
			$table->push();
		}
		echo $table->render();
	}
?>
